{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下三行是汉化过程中测试用代码，学习者在自己的环境中应删除或注释掉\n",
    "导入 sys\n",
    "导入 os\n",
    "sys.path.追加(os.路径.绝对路径(r'G:\\grasspy_zwmodules\\bs4\\zwbs\\src'))\n",
    "\n",
    "导入 网络请求\n",
    "从 蜘蛛网 导入 〇蜘蛛网\n",
    "\n",
    "响应 = 网络请求.查_get(\"https://www.baidu.com\")\n",
    "蜘蛛网 = 〇蜘蛛网(响应.内容, 特性=\"html.parser\")  # 代表被解析的文档整体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 搜索蜘蛛网（文档树）\n",
    "# 过滤器种类：\n",
    "# 1、字符串\n",
    "蜘蛛网.查找全部('a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2、正则表达式\n",
    "导入 正则\n",
    "蜘蛛网.查找全部(正则.编译('p'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3、列表\n",
    "蜘蛛网.查找全部(['a', 'title'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4、真\n",
    "# 匹配所有可以匹配的标签，文本字符串除外\n",
    "蜘蛛网.查找全部(真)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5、函数\n",
    "套路 有id和名称(标签):\n",
    "    返回 标签.具有属性('id') 且 标签.具有属性('name')\n",
    "\n",
    "蜘蛛网.查找全部(有id和名称)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "套路 无baidu(href):  # 链接中无 baidu 字样\n",
    "    返回 href and 非 正则.编译('baidu').搜索(href)\n",
    "\n",
    "蜘蛛网.查找全部(href=无baidu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>,\n",
       " <a class=\"mnav\" href=\"http://map.baidu.com\" name=\"tj_trmap\">地图</a>,\n",
       " <a class=\"mnav\" href=\"http://v.baidu.com\" name=\"tj_trvideo\">视频</a>]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "套路 前后都有超链接(标签):\n",
    "    返回 标签.上一兄弟元素 且 标签.上一兄弟元素.上一兄弟元素 且 \\\n",
    "        标签.上一兄弟元素.上一兄弟元素.名称 == 'a' 且 \\\n",
    "        标签.下一兄弟元素 且 标签.下一兄弟元素.下一兄弟元素 且 \\\n",
    "        标签.下一兄弟元素.下一兄弟元素.名称 == 'a'\n",
    "\n",
    "蜘蛛网.查找全部(前后都有超链接)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"bri\" href=\"//www.baidu.com/more/\" name=\"tj_briicon\" style=\"display: block;\">更多产品</a>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找全部(名称=None, 属性々={}, 递归=真, 字符串=None, 限值=None, **关键词参数々)\n",
    "蜘蛛网.查找全部('a', 'bri')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<p id=\"lh\"> <a href=\"http://home.baidu.com\">关于百度</a> <a href=\"http://ir.baidu.com\">About Baidu</a> </p>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找全部(id='lh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['hao123']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找全部(字符串=正则.编译('hao'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<link href=\"https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css\" rel=\"stylesheet\" type=\"text/css\"/>,\n",
       " <a href=\"http://home.baidu.com\">关于百度</a>,\n",
       " <a href=\"http://ir.baidu.com\">About Baidu</a>,\n",
       " <a href=\"http://www.baidu.com/duty/\">使用百度前必读</a>,\n",
       " <a class=\"cp-feedback\" href=\"http://jianyi.baidu.com/\">意见反馈</a>]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 蜘蛛网.查找全部(属性々={'name':False}, href=正则.编译('baidu'))\n",
    "蜘蛛网.查找全部(属性々={'name':正则.编译('tj')}, href=正则.编译('baidu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<span class=\"bg s_ipt_wr\"><input autocomplete=\"off\" autofocus=\"autofocus\" class=\"s_ipt\" id=\"kw\" maxlength=\"255\" name=\"wd\" value=\"\"/></span>,\n",
       " <span class=\"bg s_btn_wr\"><input autofocus=\"\" class=\"bg s_btn\" id=\"su\" type=\"submit\" value=\"百度一下\"/></span>,\n",
       " <input autofocus=\"\" class=\"bg s_btn\" id=\"su\" type=\"submit\" value=\"百度一下\"/>]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 依据 css 类名搜索\n",
    "蜘蛛网.查找全部(class_='bg')  # 同上，也可以使用正则表达式、函数、True/False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<input autofocus=\"\" class=\"bg s_btn\" id=\"su\" type=\"submit\" value=\"百度一下\"/>]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找全部(class_='bg s_btn')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找全部(class_='s_btn bg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<input autofocus=\"\" class=\"bg s_btn\" id=\"su\" type=\"submit\" value=\"百度一下\"/>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.选择('input.s_btn.bg')  # 使用 css 选择器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['百度一下，你就知道',\n",
       " 'document.write(\\'<a href=\"http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=\\'+ encodeURIComponent(window.location.href+ (window.location.search === \"\" ? \"?\" : \"&\")+ \"bdorz_come=1\")+ \\'\" name=\"tj_login\" class=\"lb\">登录</a>\\');\\r\\n                ',\n",
       " '关于百度',\n",
       " '使用百度前必读']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 蜘蛛网.查找全部(字符串=['新闻', '地图'])\n",
    "蜘蛛网.查找全部(字符串=[正则.编译('百度'), 正则.编译('baidu')])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['百度一下，你就知道', '关于百度']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找全部(字符串=正则.编译('百度'), 限值=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">新闻</a>,\n",
       " <a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>,\n",
       " <a class=\"mnav\" href=\"http://map.baidu.com\" name=\"tj_trmap\">地图</a>,\n",
       " <a class=\"mnav\" href=\"http://v.baidu.com\" name=\"tj_trvideo\">视频</a>,\n",
       " <a class=\"mnav\" href=\"http://tieba.baidu.com\" name=\"tj_trtieba\">贴吧</a>]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 像函数一样调用标签相当于调用'查找全部'\n",
    "# 以下两行代码等价\n",
    "蜘蛛网.div.查找全部(class_=\"mnav\")\n",
    "蜘蛛网.div(class_=\"mnav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">新闻</a>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找：返回找到的第一个元素，没有则返回空\n",
    "蜘蛛网.查找('a')  # 等同于 蜘蛛网.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">新闻</a>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "蜘蛛网.查找('div').查找('a')\n",
    "蜘蛛网.div.a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查找所有父元素 和 查找父元素\n",
    "标签 = 蜘蛛网.div.a\n",
    "标签.查找父元素()\n",
    "标签.查找父元素(id='head')\n",
    "标签.查找所有父元素()\n",
    "标签.查找所有父元素('body')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>,\n",
       " <a class=\"mnav\" href=\"http://map.baidu.com\" name=\"tj_trmap\">地图</a>,\n",
       " <a class=\"mnav\" href=\"http://v.baidu.com\" name=\"tj_trvideo\">视频</a>,\n",
       " <a class=\"mnav\" href=\"http://tieba.baidu.com\" name=\"tj_trtieba\">贴吧</a>]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找下一兄弟元素 和 查找之后所有兄弟元素\n",
    "标签.查找下一兄弟元素()  # 注意与 标签.下一兄弟元素 的不同\n",
    "标签.查找之后所有兄弟元素()\n",
    "标签.查找之后所有兄弟元素(class_=\"mnav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"mnav\" href=\"http://tieba.baidu.com\" name=\"tj_trtieba\">贴吧</a>,\n",
       " <a class=\"mnav\" href=\"http://v.baidu.com\" name=\"tj_trvideo\">视频</a>,\n",
       " <a class=\"mnav\" href=\"http://map.baidu.com\" name=\"tj_trmap\">地图</a>,\n",
       " <a class=\"mnav\" href=\"https://www.hao123.com\" name=\"tj_trhao123\">hao123</a>,\n",
       " <a class=\"mnav\" href=\"http://news.baidu.com\" name=\"tj_trnews\">新闻</a>]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找上一兄弟元素 和 查找之前所有兄弟元素\n",
    "标签2 = 标签.查找下一兄弟元素('script')\n",
    "# 标签2\n",
    "标签2.查找上一兄弟元素()\n",
    "标签2.查找之前所有兄弟元素()\n",
    "标签2.查找之前所有兄弟元素(class_=\"mnav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<p id=\"lh\"> <a href=\"http://home.baidu.com\">关于百度</a> <a href=\"http://ir.baidu.com\">About Baidu</a> </p>]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找下一元素 和 查找之后所有元素\n",
    "标签.查找下一元素()\n",
    "标签.查找下一元素(属性々={'name':'tj_trmap'})\n",
    "标签.查找之后所有元素()\n",
    "标签.查找之后所有元素(id='lh')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<input autofocus=\"\" class=\"bg s_btn\" id=\"su\" type=\"submit\" value=\"百度一下\"/>]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查找上一元素 和 查找之前所有元素\n",
    "标签2.查找上一元素()\n",
    "标签2.查找上一元素(属性々={'name':'tj_trmap'})\n",
    "标签2.查找之前所有元素()\n",
    "标签2.查找之前所有元素(id='su')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择、选一个 可以使用 css 选择器\n",
    "蜘蛛网.选择('p a')\n",
    "蜘蛛网.选一个('p a')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.1 ('.venv': venv)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1+"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9f23957215ae7e6101af7478a3aa6d946ec85d2106801d4efdfa4b8f6a5e1ee8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
